using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using JNPF.Basics.Models;
using JNPF.Basics.Logic;
using JNPF.Utils;
using System.Threading.Tasks;
using System.Linq;
using JNPF.Utils.Controller;
using JNPF.Basics.Logic.Common.Login;
using JNPF.Utils.Extensions;

namespace JNPF.Basics.Controller
{
	[Route("api/Jiushi/[Controller]")]
    public class CustomeractualcontrollerController : BaseApiController
    {
        private CustomeractualcontrollerBll CustomeractualcontrollerService=new CustomeractualcontrollerBll();
        private BillRuleBll billRuleService = new BillRuleBll();
        private DataInterfaceBll dataInterfaceBll = new DataInterfaceBll();
        private UserBll userBll = new UserBll();
        private OrganizeBll organizeBll = new OrganizeBll();


		#region GET

		/// <summary>
		/// 获取信息
		/// </summary>
		/// <param name="id">主键id</param>
		/// <returns></returns>
		[HttpGet("{id}")]
		public async Task<ResponseResult> GetInfo(int id)
		{
			var data = await CustomeractualcontrollerService.GetInfo(id);
			var output = data.MapTo<CustomeractualcontrollerUpOutput>();
			return Success(output);
		}

		/// <summary>
		/// 获取列表
		/// </summary>
		/// <param name="input">请求参数</param>
		/// <returns></returns>
		[HttpGet]
		public async Task<ResponseResult> GetList(CustomeractualcontrollerListQueryInput input)
		{
			var data = await CustomeractualcontrollerService.GetPageList(input);
			var outputList = data.list.MapToList<CustomeractualcontrollerListOutput>();
			outputList.ForEach(o =>
			{
				o.birthDate=o.birthDate !=null ? string.Format("{0:yyyy-MM-dd}",Convert.ToDateTime(o.birthDate)):null;
			});
			var output = new PageResult()
			{
				list = outputList,
				pagination = data.pagination
			};
			return Success(output);
		}

		#endregion

		#region POST

		/// <summary>
		/// 新建
		/// </summary>
		/// <param name="input">新建输入</param>
		/// <returns></returns>
		[HttpPost]
		public async Task<ResponseResult> Create([FromBody] CustomeractualcontrollerCrInput input)
		{
			var entity = input.MapTo<CustomeractualcontrollerEntity>();
			await CustomeractualcontrollerService.Create(entity);
			return Success("新建成功");
		}

		/// <summary>
		/// 导出
		/// </summary>
		/// <returns></returns>
		[HttpPost("Actions/Export")]
		public async Task<ResponseResult> Export([FromBody] CustomeractualcontrollerListQueryInput input)
		{
			var exportData = new List<CustomeractualcontrollerEntity>();
			if (input.dataType == 0)
			{
				var data = await CustomeractualcontrollerService.GetPageList(input);
				exportData = data.list;
			}
			else {
				exportData= await CustomeractualcontrollerService.GetList(input);
			}
			List<ParamsModel> paramList = "[{\"value\":\"姓名\",\"field\":\"name\"},{\"value\":\"出生日期\",\"field\":\"birthDate\"},{\"value\":\"学历\",\"field\":\"education\"},{\"value\":\"籍贯\",\"field\":\"origin\"},{\"value\":\"定居年数\",\"field\":\"yearsOfResidence\"},{\"value\":\"婚姻状况\",\"field\":\"marriageStatus\"},{\"value\":\"配偶情况\",\"field\":\"spouseSituation\"},{\"value\":\"子女情况\",\"field\":\"childrenSituation\"},{\"value\":\"家庭成员本地定居情况\",\"field\":\"familyLocalResidenceStatus\"},{\"value\":\"专业技能或个人荣誉\",\"field\":\"professionalSkillsOrHonors\"},{\"value\":\"家庭房产信息\",\"field\":\"familyPropertyInformation\"},{\"value\":\"实际控制人与法定代表人关系说明\",\"field\":\"relationshipBetweenActualControllerAndLegalRepresentative\"},{\"value\":\"实际控制人两年内是否发生过变更\",\"field\":\"hasActualControllerChangedInTwoYears\"}]".ToList<ParamsModel>();
			var userinfo = UserProvider.Instance.Get();
			ExcelConfig excelconfig = new ExcelConfig();
			excelconfig.FileName = "企业实际控制人.xls";
			excelconfig.HeadFont = "微软雅黑";
			excelconfig.HeadPoint = 10;
			excelconfig.IsAllSizeColumn = true;
			excelconfig.ColumnModel = new List<ExcelColumnModel>();
			List<string> selectKeyList = input.selectKey.Split(',').ToList<string>();
			foreach (var item in selectKeyList)
			{
				var isExist = paramList.Find(p => p.field == item);
				if (isExist != null)
				{
					excelconfig.ColumnModel.Add(new ExcelColumnModel() { Column = isExist.field.UpperFirstChar(), ExcelColumn = isExist.value });
				}
			}
			var addPath = ConfigurationKey.TemporaryFilePath + excelconfig.FileName;
			ExcelExportHelper<CustomeractualcontrollerEntity>.Export(exportData, excelconfig, addPath);
			return Success(new { name = excelconfig.FileName, url = "/api/Common/Download?encryption=" + userinfo.Id + "|" + excelconfig.FileName });
		}
		/// <summary>
		/// 编辑
		/// </summary>
		/// <param name="id">主键id</param>
		/// <param name="input">编辑输入</param>
		/// <returns></returns>
		[HttpPut("{id}")]
		public async Task<ResponseResult> Update(int id, [FromBody] CustomeractualcontrollerCrInput input)
		{
			var entity = input.MapTo<CustomeractualcontrollerEntity>();
			var isOk = await CustomeractualcontrollerService.Update(id, entity);
			return isOk > 0 ? Success("修改成功") : Error("修改失败");
		}

		/// <summary>
		/// 删除接口
		/// </summary>
		/// <param name="id">主键id</param>
		/// <returns></returns>
		[HttpDelete("{id}")]
		public async Task<ResponseResult> Delete(int id)
		{
			var entity = await CustomeractualcontrollerService.GetInfo(id);
			var isOk = await CustomeractualcontrollerService.Delete(entity);
			return isOk > 0 ? Success("删除成功") : Error("删除失败");
		}

		#endregion

	}
}